//#pragma comment (lib, "glaux.lib")
#using <system.drawing.dll>
//#pragma comment(lib, "urlmon.lib") 

#include "image.h"

#include <urlmon.h> //Must add the urlmon.h to project or URLDownloadToFile will not work.

#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library

#include <direct.h> // for getcwd
#include <stdlib.h>// for MAX_PATH
#include <iostream>
#include <fstream>
#include <vector>

#include <stdio.h>
#include < stdlib.h >
#include < vcclr.h >

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;

class picture
{
	public:
		GLfloat	xrot;				// X Rotation ( NEW )
		GLfloat	yrot;				// Y Rotation ( NEW )
		GLfloat	zrot;				// Z Rotation ( NEW )
		GLuint texture[2];			// Storage For two textures
		
		std::vector <image>			images;

public:

	picture(){}

	/*typedef void (*Funk)(LPVOID,char*,char*,int,int);

	bool Download(char* URL, char* File_Name)
	{
		HINSTANCE Dll_Handle = LoadLibrary(L"Urlmon.dll");
		if (Dll_Handle == NULL) return FALSE;
		Funk DnLd = (Funk)GetProcAddress(Dll_Handle,"URLDownloadToFileA");
		if (DnLd == NULL) return FALSE;
		DnLd(0,URL,File_Name,0,0);
		FreeLibrary(Dll_Handle);
		return TRUE;
	}*/

	void addImages(HtmlElementCollection^ list)
	{	
		//Then add to picture array
		for(int i = 0; i < list->Count; i++)
		{
			HtmlElement^ a = list[i];
			String^ s2 = a->GetAttribute("src");
			//String^ filename = gcnew String("temp\\" + s2);
			//Convert
			//const wchar_t*
			//Download the file to temp directory
			//HRESULT hr = URLDownloadToFile( NULL, wcstring, wcstring2, 0, NULL );
			//URLDownloadToFile(NULL, "http://www.cs.bris.ac.uk/home/dk5743/mascot/crown.jpg", "C:\\crown.jpg", 0, NULL );
		}
	}

	/************* LoadGLTextures *****************************/
	// Load Bitmaps And Convert To Textures
	GLvoid LoadGLTextures()									// Load Bitmaps And Convert To Textures
	{
		std::ofstream myfile;
		myfile.open("done2.txt", std::ios::app);
		myfile << "LOADTEXTURES\n";

		AUX_RGBImageRec *TextureImage[2];
		memset(TextureImage,0,sizeof(void *)*2);				// Set The Pointer To NULL

		//Load images
		//Get current path
		char path[_MAX_PATH];
		_getcwd(path, _MAX_PATH);

		  for(int i = 0; i < _MAX_PATH; i++)
		  { 
			  if(path[i] == '\\'){ path[i] = '/'; }
		  }

		  myfile << "path = " << path;
		
		image* i1 = new image("logo.bmp");
		image* i2 = new image("logo.bmp");


		  
		//This is where loadBMP should be used
		//TextureImage[0]=LoadBMP("logo.bmp");
		TextureImage[0]=i1->getImage();
		TextureImage[1]=i2->getImage();
		
		//TextureImage[0] = auxDIBImageLoad(_T"C:/Documents and Settings/Dominique/My Documents/Visual Studio 2005/Projects/webCube/webCube/logo.bmp");
		//TextureImage[1] = auxDIBImageLoad(_T"C:/Documents and Settings/Dominique/My Documents/Visual Studio 2005/Projects/webCube/webCube/computer1.bmp");
		
		  myfile << "TextureImage[0] = " << TextureImage[0];
		  myfile.close();
		

		//Check they opened correctly
		if(!TextureImage[0] || !TextureImage[1])
		{
			myfile << "++++++++++++++ Null pointer\n";	
		}
		
		//we want to build two textures, and we want the texture to be stored in texture[0], texture[1]
		glGenTextures(2, &texture[0]);

		// Loop Through All The Textures
		for(int loop=0; loop<2; loop++)	
		{
			// Build All The Textures
			glBindTexture(GL_TEXTURE_2D, texture[loop]);
			//The next two lines tell OpenGL what type of filtering to use when the image 
			//is larger (GL_TEXTURE_MAG_FILTER) or stretched on the screen than the original 
			//texture, or when it's smaller (GL_TEXTURE_MIN_FILTER) on the screen than the actual texture.
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			// Generate The Texture
			glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
		}

		//The following lines of code check to see if the bitmap data we loaded to build our textures is using up ram. If it is, the ram is freed. 
		for(int loop=0; loop<2; loop++)
		{
	        if(TextureImage[loop])						// If Texture Exists
			{
				if(TextureImage[loop]->data)			// If Texture Image Exists
				{
					free(TextureImage[loop]->data);		// Free The Texture Image Memory
				}
				free(TextureImage[loop]);			// Free The Image Structure
			}
		}								// Return The Status

		 myfile << "DONE LOADTEXTURES\n";
		     myfile.close();
	}

////DRAW THE SCENE//////////

	GLvoid DrawGLSceneCube(GLvoid)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
		glLoadIdentity();										// Reset The View
		glTranslatef(0.0f,0.0f,-5.0f);

		glBindTexture(GL_TEXTURE_2D, texture[0]);

		glBegin(GL_QUADS);
			// Front Face
			//glNormal3f( 0.0f, 0.0f, 1.0f);
			glTexCoord2f(0.0f, 1.0f); 
			glVertex3f(-1.0f,  1.0f,  0.0f);
			glTexCoord2f(1.0f, 1.0f); 
			glVertex3f( 1.0f,  1.0f,  0.0f);
			glTexCoord2f(1.0f, 0.0f); 
			glVertex3f( 1.0f, -1.0f,  0.0f);
			glTexCoord2f(0.0f, 0.0f); 
			glVertex3f(-1.0f, -1.0f,  0.0f);
		glEnd();
	}

	GLvoid DrawGLScene(GLvoid)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
		glLoadIdentity();										// Reset The View
		
		for(int x = 0; x < 2; x++)
		{
			glBindTexture(GL_TEXTURE_2D, texture[x]);
			glBegin(GL_QUADS);

			glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(x), 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x+1), float(x), 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x+1), float(x+1), 0.0f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(x+1), 0.0f);
			glEnd();
		}
	}

//Depreciated!!!!!!!!!!!!!!!!!!!!
int draw_tiles(GLvoid)
{
	int map[10][10] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						   {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
						   };
  int tile;
	for (int y = 0; y < 10; y++)
  {
    for (int x = 0; x < 10; x++)
    {
		tile = map[y][x];
		glBindTexture(GL_TEXTURE_2D, texture[tile]);
								// Keep Going
		 glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f);
    glEnd();

    }
  }
  return (1);
}

};